package com.kgc.rb.service.impl;

import com.alibaba.dubbo.config.annotation.Reference;
import com.kgc.rb.common.tools.Constants;
import com.kgc.rb.common.tools.EmptyUtils;
import com.kgc.rb.dto.RbResult;
import com.kgc.rb.dto.vo.GoodsVO;
import com.kgc.rb.dto.vo.OrderVo;
import com.kgc.rb.exception.OrderException;
import com.kgc.rb.pojo.QgGoods;
import com.kgc.rb.pojo.QgOrder;
import com.kgc.rb.pojo.QgUser;
import com.kgc.rb.service.LocalOrderService;
import com.kgc.rb.service.QgGoodsService;
import com.kgc.rb.service.QgOrderService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class LocalOrderServiceImpl implements LocalOrderService {

    @Reference(check = false)
    private QgOrderService qgOrderService;
    @Reference(check = false)
    private QgGoodsService qgGoodsService;


    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public RbResult queryOrderListByUser(String token) throws Exception {
        List<OrderVo> orderVoList = null;
        //1.根据token获取等的用户的信息
        QgUser qgUser = (QgUser) redisTemplate.opsForValue().get(token);
        //2.根据用户编号查询该用户下的订单信息
        Map<String, Object> params = new HashMap<>();
        params.put("userId", qgUser.getId());
        List<QgOrder> qgOrderList = qgOrderService.getQgOrderListByMap(params);
        if (EmptyUtils.isEmpty(qgOrderList) || qgOrderList.size() == 0) {//没有查询到该用户下的订单信息列表
            return RbResult.ok(orderVoList);
        }
        orderVoList = new ArrayList<>();
        for (QgOrder order : qgOrderList) {
            //4.封装OrderVO对象
            OrderVo orderVo = new OrderVo();
            BeanUtils.copyProperties(order, orderVo);
            //3.根据商品编号查询商品图片
            GoodsVO goodsVO = (GoodsVO) redisTemplate.opsForValue().get(Constants.GOODS_PREFIX + order.getGoodsId());
            if (goodsVO != null) {//说明根据商品编号在redis中查询到该商品的详细信息
                orderVo.setGoodsImg(goodsVO.getGoodsImg());
            } else {//说明根据商品编号在redis中没有查询到该商品的详细信息
                //根据商品编号去数据库中查询
                QgGoods qgGoods = qgGoodsService.getQgGoodsById(order.getId());
                orderVo.setGoodsImg(qgGoods.getGoodsImg());
            }
            orderVoList.add(orderVo);
        }
        return RbResult.ok(orderVoList);
    }

    /**
     * 根据订单编号查询订单信息（订单编号和订单金额）
     *
     * @param token
     * @param orderId
     * @return
     * @throws Exception
     */
    @Override
    public RbResult findOrderByCondition(String token, String orderId) throws Exception {
        //1.根据token获取等的用户的信息
        QgUser qgUser = (QgUser) redisTemplate.opsForValue().get(token);
        //2.根据订单编号查询订单信息
        QgOrder qgOrder = qgOrderService.getQgOrderById(orderId);
        //3.判断是否是非法的请求
        if (EmptyUtils.isEmpty(qgOrder) || !qgOrder.getUserId().equals(qgUser.getId())) {//认为这个请求是一个非法的请求
            return RbResult.build(OrderException.ORDER_NOT_EXIST.getCode(),
                    OrderException.ORDER_NOT_EXIST.getMessage());
        }
        return RbResult.ok(qgOrder);
    }
}
